/**
 * 设置cookie
 * @param {string} key  cookie的名字
 * @param {string} value 要设置的cookie的值
 * @param {int} expires 过期时间 生存的时间毫秒
 */
function setCookie(key, value, expires) {
    let str = `${key}=${value};expires=`;
    if (expires) {
        let pastTime = new Date().getTime() - 8 * 3600 * 1000 + expires;
        let expireTime = new Date(pastTime).toString();
        // console.log(expireTime);
        str += expireTime;
    }
    str +="; path=/; domain=localhost;"; // 指定了cookie的path
    document.cookie = str;
}

/**
 * 查询cookie
 * @param {string} key 要查询的键
 * @returns 对应的cookie的值
 */
function getCookie(key) {
    //a=100; b=200; c=300
    let cookies = document.cookie.split("; ");
    for (let i = 0; i < cookies.length; i++) {
        let kv = cookies[i].split("=");
        if (kv[0] === key) {
            return kv[1];
        }
    }
    return null;
}

/**
 * 删除cookie
 * @param {string} key 要删除的cookie
 */
function deleteCookie(key) {
    document.cookie = `${key}="fuckoff";expires=`+new Date(new Date().getTime() - 8*3600*1000 -1000);
}
